package com.ttl.bc.report;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.jasperreports.engine.JasperRunManager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.stock.request2.bean.BCStkIssue2;

import com.ttl.bc.stock.request2.db.BCStkIssue2BO;
import com.ttl.common.util.CommonUtils;



public class ReportAction extends DispatchAction {
	
	public ActionForward blank(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		@SuppressWarnings("unused")
		String typeReport = (String)request.getAttribute("type");		
		//System.out.println("typeReport = "+typeReport);
		
		return mapping.findForward("blank_distribution");
	}

	@SuppressWarnings("unchecked")
	public ActionForward showDistribute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		DynaActionForm generateReportForm = (DynaActionForm)form;
		String monthId = generateReportForm.getString("monthId");
		String yearId = generateReportForm.getString("yearId");
		String ttlStr = generateReportForm.getString("ttlStr");
		String monthStr[] = { "January","February", "March", "April", "May", "June", 
							  "July","August", "September", "October", "November", "December"};
		
		HashMap hashMap = new HashMap();
		hashMap.put("monthId", monthId);
		hashMap.put("yearId", yearId);
		hashMap.put("monthStr", monthStr[Integer.parseInt(monthId)-1]);
		
		Connection connection;
		ServletOutputStream servletOutputStream = response.getOutputStream();
		
		InputStream reportStream;				
		
		if (ttlStr.equalsIgnoreCase("TTL-1")) {
			reportStream = getServlet().getServletConfig()
							.getServletContext().getResourceAsStream("/ireport/rptDistribution2Crosstab.jasper");
		}else {
			reportStream = getServlet().getServletConfig()
							.getServletContext().getResourceAsStream("/ireport/rptDistribution2CrosstabTTL2.jasper");
		}		
		
		response.setContentType("application/pdf");
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		
		connection = DriverManager.getConnection("jdbc:odbc:ttl51","sa","admin@123");
		
		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);				
		
		connection.close();
		servletOutputStream.flush();
		servletOutputStream.close();
						
		return mapping.getInputForward();		
		//return mapping.findForward("blank_distribution");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward printStockRequest(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		@SuppressWarnings("unused")
		DynaActionForm stkReq = (DynaActionForm)form;
		String docNoParam = (String)request.getParameter("docNo");

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :ReportAction #mode :printStockRequest ");		
		System.out.println("#docNo = "+docNoParam);
		
		String hql = "from BCStkIssue2 where docNo = '"+docNoParam+"'";
		BCStkIssue2BO bo = new BCStkIssue2BO();
		ArrayList results = (ArrayList)bo.findPrint(hql);

		String nameOfReport = "";
		
		if (results != null ) {
			BCStkIssue2 s2 = (BCStkIssue2)results.get(0);			
			
			if (s2.getBcStkIssueSub2s().size()==1) {
				nameOfReport = "/ireport/rptStockRequest_1lineItem_v3_win7.jasper";
			}else {
				nameOfReport = "/ireport/rptStockRequest_v3_win7.jasper";
			}
		}
					
		HashMap hashMap = new HashMap();
		hashMap.put("docNoParam", docNoParam);
		
		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}		
//		Connection connection;
//		ServletOutputStream servletOutputStream = response.getOutputStream();
//		
//		InputStream reportStream = getServlet().getServletConfig()
//							.getServletContext().getResourceAsStream(nameOfReport);
//
//		
//		response.setContentType("application/pdf");
//		
//		//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//		Class.forName("net.sourceforge.jtds.jdbc.Driver");		
////		connection = DriverManager.getConnection("jdbc:odbc:ttl51","sa","admin123");
//		connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.2:1433/ttl51;user=sa;password=admin@123");
////		System.out.println("connect jTDS ok");
//		
//		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);
//		//System.out.println("runReportToPdfStream ok!!");
//		connection.close();
//		servletOutputStream.flush();
//		servletOutputStream.close();
		//System.out.println("before return report");
		//System.out.println("mapping.getInputForward()="+mapping.getInputForward());
		return null;			
	}		
}
